package com.atguigu.gulimall.ware.service.impl;

import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;
import com.atguigu.common.vo.WareStock;
import com.atguigu.gulimall.ware.dao.WareSkuDao;
import com.atguigu.gulimall.ware.entity.WareSkuEntity;
import com.atguigu.gulimall.ware.service.WareSkuService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("wareSkuService")
public class WareSkuServiceImpl extends ServiceImpl<WareSkuDao, WareSkuEntity> implements WareSkuService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<WareSkuEntity> page = this.page(
                new Query<WareSkuEntity>().getPage(params),
                new QueryWrapper<WareSkuEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 查询sku商品是否有库存
     *
     * @param skuIds
     * @return
     */
    @Override
    public List<WareStock> getSkuStock(List<Long> skuIds) {
        List<WareStock> wareStocks = skuIds.stream()
                .map(skuId -> {
                    WareStock wareStock = new WareStock();
                    //查每个skuid的库存
                    Long count = baseMapper.getSkuStock(skuId);
                    if (count == null) {
                        count = 0L;
                    }
                    wareStock.setSkuId(skuId);
                    wareStock.setHasStock(count > 0);
                    return wareStock;
                }).collect(Collectors.toList());
        return wareStocks;
    }

}